Imports System.Data
Imports System.Security.Cryptography

Partial Class Client_frmChangePassword
    Inherits System.Web.UI.Page
    Dim _UserId As String
    Dim dt As New DataTable
    Dim _bo As New Bo()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _UserId = Session("UserId")
        If _UserId = "" Then
            Response.Redirect("frmLogin.aspx")
        End If
    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        Dim md5Hasher As MD5 = MD5.Create()
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.[Default].GetBytes(txtOldPassword.Text))
        Dim sBuilder As New StringBuilder()
        For i As Integer = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next

        dt = _bo.getPassword(_UserId)
        If (sBuilder.ToString() = dt.Rows(0).Item(0).ToString And txtOldPassword.Text <> txtNewPassword.Text) Then

            Dim newData As Byte() = md5Hasher.ComputeHash(Encoding.[Default].GetBytes(txtNewPassword.Text))
            Dim newSBuilder As New StringBuilder()
            For i As Integer = 0 To data.Length - 1
                sBuilder.Append(data(i).ToString("x2"))
            Next

            _bo.UpdatePassword(newSBuilder.ToString(), _UserId)
            MsgBox("New Password Is Updated To your Account")
            Session("UserId") = _UserId
            Response.Redirect("frmDataList.aspx")
        Else
            If (sBuilder.ToString() <> dt.Rows(0).Item(0).ToString) Then
                MsgBox("Old Password Does Not Match")
                Call clear()
            ElseIf (txtOldPassword.Text = txtNewPassword.Text) Then
                MsgBox("New Password Cannot Be Same As Old Password")
                Call clear()
            End If
        End If
    End Sub
    Sub clear()
        txtNewPassword.Text = ""
        txtOldPassword.Text = ""
        txtRetypePassword.Text = ""
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Call clear()
    End Sub
End Class
